
//
//  PlatformHelper.m
//  Unity-iPhone
//
//  Created by violintel on 25/04/2018.
//

#import <Foundation/Foundation.h>
#import <AdSupport/AdSupport.h>
#import <sys/utsname.h>
#import "UnityAppController.h"
#include <string>
#import "MFMailResultListener.h"
#import "Libraries/Plugins/Editor/XUPorter/Mods/AppFlyer/AppsFlyerTracker.h"
//#import <Crashlytics/Crashlytics.h>
#import <mach/mach.h>
#import "NativeFBShareDelegate.h"

#if defined(__cplusplus)
extern "C"{
#endif
    const char* GetIDFA() {
        NSString *adId = @"";
        if([[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled])
        {
            NSUUID *IDFA = [[ASIdentifierManager sharedManager] advertisingIdentifier];
            adId = [IDFA UUIDString];
            NSLog(@"isAdvertisingTrackingEnabled = true get idfa");
        }
        else
        {
            NSUUID *idfv = [[UIDevice currentDevice] identifierForVendor];
            adId = [idfv UUIDString];
            NSLog(@"isAdvertisingTrackingEnabled = false get idfv");
        }
        char *result = (char *)malloc(strlen([adId UTF8String]) + 1);
        strcpy(result, [adId UTF8String]);
        return result;
    }
    
    bool IsIphoneX() {
        struct utsname systemInfo;
        uname(&systemInfo);
        NSString *platform = [NSString stringWithCString:systemInfo.machine
                                                encoding:NSUTF8StringEncoding];
        if ([platform isEqualToString:@"iPhone10,3"])    return true;
        if ([platform isEqualToString:@"iPhone10,6"])    return true;
        
        return  false;
    }
    
    void openURL(const char* url) {
        NSString *str = [[NSString alloc] initWithCString:url encoding:NSUTF8StringEncoding];
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:str]];
    }
    
    const char* getLocalLanguage() {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSArray *appLanguages = [defaults objectForKey:@"AppleLanguages"];
        NSString *languageName = [appLanguages objectAtIndex:0];
        char *result = (char *)malloc(strlen([languageName UTF8String]) + 1);
        strcpy(result, [languageName UTF8String]);
        return result;
    }
    
    void contactus(const char* destination, const char* title, const char* content) {
        std::string toAddressStr = destination;
        std::string subjectStr = title;
        NSString *toAddress = [[NSString alloc] initWithUTF8String:toAddressStr.c_str()];
        NSString *subject = [[NSString alloc] initWithUTF8String:subjectStr.c_str()];
        NSString *body = [[NSString alloc] initWithUTF8String:content];
        
        MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
        static MFMailResultListener* mailResultListener = [[MFMailResultListener alloc] init];
        controller.mailComposeDelegate = mailResultListener;
        NSArray *usersTo = [NSArray arrayWithObject:toAddress];
        [controller setToRecipients:usersTo];
        [controller setSubject:subject];
        [controller setMessageBody:body isHTML:NO];
        
        if (controller)  [GetAppController().rootViewController presentViewController:controller animated:YES completion:nil];
    }
    
    const char* getAppVersion() {
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
        char *result = (char *)malloc(strlen([app_Version UTF8String]) + 1);
        strcpy(result, [app_Version UTF8String]);
        return result;
    }
    
    const char* getAppsFlyerUID() {
        NSString* uid = [[AppsFlyerTracker sharedTracker] getAppsFlyerUID];
        char *result = (char *)malloc(strlen([uid UTF8String]) + 1);
        strcpy(result, [uid UTF8String]);
        return result;
    }
    
    void setFirebaseUserId(const char* userIdInfo) {
        NSLog(@"setFirebaseUserId: %s", userIdInfo);
        NSString* uidInfo = [[NSString alloc] initWithCString:userIdInfo encoding:NSUTF8StringEncoding];
        //[CrashlyticsKit setUserIdentifier:uidInfo];
    }
    
    long _IOS_GetTaskUsedMemeory()
    {
        task_basic_info_data_t taskInfo;
        mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;
        kern_return_t kernReturn = task_info(mach_task_self(),
                                             TASK_BASIC_INFO,
                                             (task_info_t)&taskInfo,
                                             &infoCount);
        if (kernReturn == KERN_SUCCESS) {
            long usedMemory = taskInfo.resident_size/1024.0/1024.0;
            NSLog(@"TaskUsedMemeory: %ld", usedMemory);
            return usedMemory;
        }
        NSLog(@"TaskUsedMemeory: %ld", 0L);
        return 0;
    }
    
    void setCustomerUserID(const char* cuid)
    {
        NSLog(@"setCustomerUserID: %s", cuid);
        NSString* customUserID = [[NSString alloc] initWithCString:cuid encoding:NSUTF8StringEncoding];
        [AppsFlyerTracker sharedTracker].customerUserID = customUserID;
    }
    
    const BOOL isAppleSinginAvaliable()
    {
        if(@available(iOS 13.0, tvOS 13.0, *))
            return true;
        return false;
    }



    void FacebookShare(const char* imgPath, const char* caption)
    {
        UIImage* image = [UIImage imageWithContentsOfFile:[[NSString alloc]initWithUTF8String:imgPath]];
        FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init];
        photo.image = image;
        photo.caption = [[NSString alloc]initWithUTF8String:caption];
        photo.userGenerated = YES;
        FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
        content.photos = @[photo];
        
        static NativeFBShareDelegate* shareResultDelegate = [[NativeFBShareDelegate alloc] init];
        
        [FBSDKShareDialog showFromViewController:UnityGetGLViewController()
                                      withContent:content
                                         delegate:shareResultDelegate];
    }

    bool CheckPackageInstalled(const char* packageName)
    {
        NSString *str = [[NSString alloc] initWithCString:packageName encoding:NSUTF8StringEncoding];
        NSURL *packageURL = [NSURL URLWithString:str];//or whatever url you're checking
        if ([[UIApplication sharedApplication] canOpenURL:packageURL])
        {
            return true;
        }
        return false;
    }
#if defined(__cplusplus)
}
#endif
